import { combineReducers } from 'redux'
import { ADD_TODO, SET_VISIBILITY_FILTER, VisibilityFilters, COMPLETE_TODO } from './action';
const { SHOW_ALL } = VisibilityFilters
function visibilityFilter(state = SHOW_ALL, action)
{
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return action.filter
    default:
      return state
  }
}


let i = 0;
function todos(state = [], action)
{
  switch (action.type) {
    case ADD_TODO:
      i++;
      return [
        ...state,
        {
          text: action.text,
          completed: false,
          id: i - 1
        }
      ]
    case COMPLETE_TODO:
      return [
        ...state.slice(0, action.index),
        Object.assign({}, state[action.index], {
          completed: !state[action.index].completed
        }),
        ...state.slice(action.index + 1)
      ]
    default:
      return state
  }
}

export const todoApp = combineReducers({
  visibilityFilter,
  todos
})